package test;

class Node {
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}

public class MyLinkedList {
    public Node head;

    public void createList() {
        Node node1 = new Node(12);
        Node node2 = new Node(3);
        Node node3 = new Node(5);
        Node node4 = new Node(2);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node1;
    }

    public void show() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        }
        node.next = this.head;
        this.head = node;
    }

    public void addLast(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        }
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    public Node searchNode(int index) {
        Node cur = this.head;
        int count = 0;
        while (count != index) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("下标不合法！");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Node cur = searchNode(index);
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
    }

    public Node searchPrevNode(int data) {
        Node cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == data) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现val的节点
    public void remove(int val) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == val) {
            this.head = this.head.next;
            return;
        }
        Node cur = searchPrevNode(val);
        if (cur == null) {
            System.out.println("没有你要删除的节点!");
            return;
        }
        Node del = cur.next;
        cur.next = del.next;
    }

    //删除所有值是val的节点
    public void removeAllKey(int val) {
        if (this.head == null) {
            return;
        }
        Node pre = this.head;
        Node cur = this.head.next;

        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }

        if (this.head.val == val) {
            this.head = this.head.next;
        }
    }

    //删除所有节点
    public void clear() {
        while (this.head != null){
            Node curNext = this.head.next;
            this.head.next = null;
            this.head = curNext;
        }
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.createList();
        myLinkedList.show();
        System.out.println(myLinkedList.size());
        System.out.println(myLinkedList.contains(12));
        System.out.println(myLinkedList.contains(22));
        myLinkedList.addFirst(13);
        myLinkedList.show();
        myLinkedList.addLast(4545);
        myLinkedList.show();
        System.out.println(myLinkedList.searchNode(3).val);
        myLinkedList.addIndex(1, 454545);
        myLinkedList.show();
        System.out.println(myLinkedList.searchPrevNode(12).val);
        myLinkedList.remove(4545);
        myLinkedList.show();
        myLinkedList.remove(13);
        myLinkedList.show();
        System.out.println("==============================");
        myLinkedList.clear();
        myLinkedList.show();
    }
}
